//get的基础自定义


import {
	HOST
} from './host'

//import vue from 'vue'
import store from '../store/index.js'

function err_dispose(err) {
	//请求状态异常处理函数
	console.log("异常")
	if (err.status == 500) {
		setTimeout(() => {
			uni.showToast({
				title: '请求失败请检查网络',
				icon: "none",
				duration: 3000
			})
		}, 500)



	} else if (err.status == 403) {
		console.log("没登录没权限")
		setTimeout(() => {
			uni.showToast({
				title: err.msg,
				icon: "none",
				duration: 3000
			})
		}, 500)

		store.commit("logout")
		uni.removeStorageSync("token")

	} else {


		// uni.showToast({
		// 	title: '未知错误',

		// 	duration: 3000
		// })

		setTimeout(() => {
			uni.showToast({
				title: err.mes,
				icon: "none",
				duration: 3000
			})

		}, 500)

	}
}

function res_dispose(res, resolve, reject) {
	//自定义业务返回信息判断
	console.log("业务返回信息判断")
	let data = res.data
	console.info('信息', data.code)

	if (data.code) {
		switch (data.code) {
			case 200:
				console.log("操作成功")
				resolve(res)
				break;
			case 201:
				console.log("对象创建成功")
				resolve(res)
				break;
			case 202:
				console.log("请求已经被接受")
				resolve(res)
				break;
			case 203:
				console.log("操作已经执行成功，但是没有返回数据")
				resolve(res)
				break;
			case 204:
				console.log("操作已经执行成功，但是没有返回数据")
				resolve(res)
				break;
			case 403:
				console.log("没登录没权限")
				setTimeout(() => {
					uni.showToast({
						title: data.msg,
						icon: "none",
						duration: 3000
					})

				}, 500)

				store.commit("logout")
				uni.removeStorageSync("token")
				reject("没登录没权限")
				break;
			default:

				if (data.status == 'success') {

					setTimeout(() => {
						uni.showToast({
							title: data.msg,
							icon: "none",
							duration: 3000
						})

					}, 500)

					resolve(res)
				} else if (data.status == 'fail') {
					if (Array.isArray(data.msg)) {
						let mes = ''
						data.msg.forEach(item => {
							mes = mes + "<p style='padding:3px 0;'>" + item.message + '</p>'
						})
						uni.showToast({
							title: mes,
							icon: "none",
							duration: 5000
						})


					} else {

						setTimeout(() => {
							uni.showToast({
								title: data.msg,
								icon: "none",
								duration: 3000
							})

						}, 500)


					}
					reject(res)
				} else if (data.status == 'err') {
					if (data.msg && data.msg.errors) {

						// uni.showToast({
						// 	title:data.msg.errors[0].message,
						// icon :"none",
						// 	mask:true,
						// 	duration:5000
						// })
						reject(res)
					} else {

						setTimeout(() => {
							uni.showToast({
								title: '未知错误',
								icon: "none",
								duration: 3000
							})

						}, 500)


						reject(res)
					}
				} else {
					console.log('信息2', data.msg)
					uni.showToast({
						title: data.msg,
						icon: "none",
						duration: 3000
					})
					setTimeout(() => {
						uni.showToast({
							title: data.msg,
							icon: "none",
							duration: 3000
						})

					}, 500)
					reject(res)
				}

				break;

		}


	} else {

		setTimeout(() => {
			uni.showToast({
				title: data.msg,
				icon: "none",
				duration: 3000
			})

		}, 500)
		reject(res)
	}

}

// let token = uni.getStorageSync("token") || "" //jwt 登录模式 token
const headers = {
	// "jp-bookstore-token": token
} //全局头

console.log("headers", headers)
const baseURL = HOST //全局域名

let count = 0 //请求计数
let loadingTimeout = -1 //请求定时器
export default {

	get: (url, info) => {
		//get请求    

		return new Promise(function(resolve, reject) {
			uni.hideLoading();

			if (count < 1) {

				loadingTimeout = setTimeout(function() {
					uni.showLoading({
						title: "加载中...",
						mask: true,
					})
				}, 5000);
				// console.log("请求开始", loadingTimeout)
			}
			count++
			//get请求
			uni.request({
				method: "GET",
				url: `${baseURL}${url}`,
				header: { ...headers,
					"jp-bookstore-token": uni.getStorageSync("token") || ""
				},
				data: info,
				timeout: 60000,
				success: (res) => {
					res_dispose(res, resolve, reject)
					// console.log(res)
					count--

					if (count < 1) {
						// console.log("请求结束", loadingTimeout)
						uni.hideLoading();
						if (loadingTimeout != -1)
							clearTimeout(loadingTimeout)
					}

				},
				fail: (err) => {
					err_dispose(err) //错误处理
					reject(err)
					count--

					if (count < 1) {
						uni.hideLoading();
						if (loadingTimeout)
							clearTimeout(loadingTimeout)
					}


				}

			})

		})

	},
	post: (url, info) => {
		//post请求
		// console.log("post", this)

		return new Promise(function(resolve, reject) {
			uni.hideLoading();
			if (count < 1)
				loadingTimeout = setTimeout(function() {
					uni.showLoading({
						title: "加载中...",
						mask: true,
					})
				}, 1000);
			count++
			uni.request({
				method: "POST",
				url: `${baseURL}${url}`,
				header: { ...headers,
					"jp-bookstore-token": uni.getStorageSync("token") || "",
					'Accept': 'application/json, text/javascript, */*; q=0.01',
					// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' //(java后台需要)
				},
				data: info,
				timeout: 60000,
				success: (res) => {
					res_dispose(res, resolve, reject)
					// console.log(res)
					count--
					if (count < 1) {
						uni.hideLoading();
						if (loadingTimeout)
							clearTimeout(loadingTimeout)
					}
				},
				fail: (err) => {
					err_dispose(err) //错误处理
					reject(err)
					count--
					if (count < 1) {
						uni.hideLoading();
						if (loadingTimeout != -1)
							clearTimeout(loadingTimeout)
					}

				}
			})

		})

	}
}

// export default function(type, url, info) {
//     return new Promise(function(resolve, reject) {

//         Indicator.open({
//             text: '加载中...',
//             spinnerType: 'fading-circle'
//         })

//         if (type == 'get') {
//             //get请求
//             axios.get(url, { params: info }).then(
//                 res => {
//                     resolve(res)
//                         // console.log(res)
//                     Indicator.close()

//                 }
//             ).catch(
//                 err => {
//                     err_dispose(err) //错误处理
//                     reject(err)
//                     Indicator.close()
//                 }
//             )
//         } else if (type == 'post') {
//             //post请求
//             axios.post(url, qs.stringify(info), {
//                 headers: {
//                     'Accept': 'application/json, text/javascript, */*; q=0.01',
//                     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
//                 },
//                 // params: info,
//                 // paramsSerializer: function(params) {
//                 //     return Qs.stringify(params, { arrayFormat: 'brackets' })
//                 // },
//             }).then(
//                 res => {
//                     resolve(res) //解析结果为json
//                     Indicator.close()
//                 }
//             ).catch(
//                 err => {
//                     Indicator.close()
//                     err_dispose(err)
//                     reject(err)
//                 }
//             )

//         }

//     })
// }
